package com.matriq.cloudmanager;

import java.util.List;

import com.matriq.cloudmanager.R;
import com.matriq.cloudmanager.entity.Comment;
import com.matriq.cloudmanager.entity.Console;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import net.lgpage.activity.BaseActivity;
import net.lgpage.util.IntentUtil;
import net.lgpage.util.ObjectRandom;
import net.lgpage.util.ViewGroupAdapter;
import net.lgpage.util.Checker.StringChecker;

public class ConsoleDetailActivity extends BaseActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_console_detail);
		setViews();
		setEvents();
		loadData();
	}

	ImageView iv_top;
	LinearLayout ll_comment;
	ImageView iv[] = new ImageView[3];
	TextView tv[] = new TextView[6];
	TextView tv_title;
	TextView tv_zannum;
	EditText et;
	ImageButton ll_back;

	private void setViews() {
		iv_top = (ImageView) this.findViewById(R.id.iv_top);
		ll_comment = (LinearLayout) this.findViewById(R.id.ll_comment);
		iv[0] = (ImageView) this.findViewById(R.id.iv0);
		iv[1] = (ImageView) this.findViewById(R.id.iv1);
		iv[2] = (ImageView) this.findViewById(R.id.iv2);
		tv[0] = (TextView) this.findViewById(R.id.tv0);
		tv[1] = (TextView) this.findViewById(R.id.tv1);
		tv[2] = (TextView) this.findViewById(R.id.tv2);
		tv[3] = (TextView) this.findViewById(R.id.tv3);
		tv[4] = (TextView) this.findViewById(R.id.tv4);
		tv[5] = (TextView) this.findViewById(R.id.tv5);
		tv_zannum = (TextView) this.findViewById(R.id.tv_zannum);
		tv_title = (TextView) this.findViewById(R.id.tv_title);
		et = (EditText) this.findViewById(R.id.et);
		ll_back = (ImageButton) this.findViewById(R.id.ll_back);
		adp_comment = new ViewGroupAdapter(ll_comment) {
			
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				View v = getLayoutInflater().inflate(R.layout.rcy_comment, null);
				ImageView iv = (ImageView) v.findViewById(R.id.iv);
				TextView tv[] = new TextView[3];
				tv[0] = (TextView) v.findViewById(R.id.tv0);
				tv[1] = (TextView) v.findViewById(R.id.tv1);
				tv[2] = (TextView) v.findViewById(R.id.tv2);
				final Comment obj = comments.get(position);
				tv[0].setText(obj.getComName());
				tv[1].setText(obj.getTime());
				tv[2].setText(obj.getContent());
				return v;
			}
			
			@Override
			public int getCount() {
				if(comments == null)return 0;
				return comments.size();
			}
		};
	}

	ViewGroupAdapter adp_comment;
	private void setEvents() {
		iv[2].setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(IntentUtil.getShareIntent("分享这篇微博", "岁的富翁额额温柔"));
			}
		});
	}

	Object copyObj;

	public boolean checkEts() {
		if (copyObj == null) {
			copyObj = new Object();
		}

		String str_et = et.getEditableText().toString();

		if (StringChecker.isNull(str_et)) {
			setError("不能为空", et);
			return false;
		}

		return true;
	}


	Console obj;

	public void setData(Console obj) {
		this.obj = obj;
		if(TextUtils.isEmpty(obj.getHeadpic())){
			iv_top.setVisibility(View.GONE);
		}else{
			iv_top.setVisibility(View.VISIBLE);
			loadImage(obj.getHeadpic(), iv_top);
		}
	
		tv[0].setText(obj.getTitle());
		tv[1].setText(obj.getPutTime());
		tv[2].setText(obj.getSource());
		tv[3].setText(obj.getContent());
		tv[4].setText(obj.getCommentNum());
		tv[5].setText(obj.getZanNum());
		tv_title.setText("咨询");
		et.setSelection(et.length());
	}

	List<Comment>comments;
	private void loadData() {
		setData((Console) intentGetObj(Console.class));
		comments = ObjectRandom.randomList(Comment.class);
		adp_comment.notifyDataSetChanged();
	}

	@Override
	public void onDestroy() {
		
		super.onDestroy();
	}

}